package com.situ.web.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.situ.web.dao.ICourseDao;
import com.situ.web.entity.Course;
import com.situ.web.util.JDBCUtil;

public class CourseDaoImpl implements ICourseDao {

	@Override
	public ArrayList<Course> selectCourse() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Course> list = new ArrayList<Course>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "Select * from Course ";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				String name = resultSet.getString("name");
				int id = resultSet.getInt("id");
				int credit = resultSet.getInt("credit");
				Course course = new Course(id,name,credit);
				list.add(course);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return list;
	
	}

	@Override
	public int insertBanjiIdCourseId(int banjiId, int courseId) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO banji_course(banji_id,course_id) VALUES(?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, banjiId);
			preparedStatement.setInt(2, courseId);
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return count;
	}

}
